package com.instagram.instadownloader.fragment;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout.LayoutParams;

import com.instagram.instadownloader.MainActivity.FragmentLifecycle;
import com.vine.vinedownloader.R;
import com.instagram.instadownloader.bookmark.BookmarkObject;
import com.instagram.instadownloader.bookmark.BookmarkViewBlock;
import com.instagram.instadownloader.bookmark.BookmarksManager;
import com.instagram.instadownloader.bookmark.OnAddBookmarkListener;
import com.instagram.instadownloader.fragment.base.InstagramFragment;
import com.instagram.instadownloader.fragment.dialog.BasicStepsDialogFragment;
import com.instagram.instadownloader.utils.ClipboardUtil;

/**
 * The Class BookMarkFragment.
 * 
 * @author HuanND
 */
public class BookMarkFragment extends InstagramFragment implements FragmentLifecycle, OnAddBookmarkListener {

	private ViewGroup wrappBookmark;
	private List<BookmarkObject> bookmarkDatas;
	private List<BookmarkObject> bookmarkShowed;

	public void addBookmarkShowed(BookmarkObject obj) {
		if (bookmarkShowed == null) {
			bookmarkShowed = new ArrayList<BookmarkObject>();
		}
		bookmarkShowed.add(obj);
	}

	@Override
	protected View onCreateContentView(LayoutInflater inflater, ViewGroup container) {
		View v = inflater.inflate(R.layout.fragment_bookmark_tab, container, false);

		wrappBookmark = (ViewGroup) v.findViewById(R.id.wrappBookmark);

		v.findViewById(R.id.btnViewBasicSteps).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				BasicStepsDialogFragment dialog = new BasicStepsDialogFragment();
				dialog.show(getFragmentManager(), getTag());
			}
		});
		v.findViewById(R.id.btnCopyOrShare).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				ClipboardUtil.putText(getActivity(), getResources().getString(R.string.guide_hint_copy1));
				ClipboardUtil.putText(getActivity(),"https://vine.co/v/hgbAalELPJP");
			}
		});
		getInstaActivity().setAddBookmarkListener(this);
		bookmarkDatas = BookmarksManager.getInstance().getBookmarks();
		if (bookmarkDatas != null && !bookmarkDatas.isEmpty()) {
			for (int i = 0; i < bookmarkDatas.size(); i++) {
				addNewBookmark(i);
			}
		}

		return v;
	}

	/**
	 * Copy to clipboard.
	 * 
	 * @param paramString
	 *            the param string
	 */
	// @SuppressLint({"NewApi"})
	// private void copyToClipboard(String paramString)
	// {
	// if (Build.VERSION.SDK_INT < 11)
	// {
	// ((ClipboardManager)getActivity().getSystemService("clipboard")).setText(paramString);
	// return;
	// }
	// ((ClipboardManager)getActivity().getSystemService("clipboard")).setPrimaryClip(ClipData.newPlainText("share url",
	// paramString));
	// }

	@Override
	public void onPauseFragment() {
	}

	@Override
	public void onResumeFragment() {
	}

	private void addNewBookmark(int pos) {
		BookmarkViewBlock bm = (BookmarkViewBlock) LayoutInflater.from(getActivity()).inflate(
				R.layout.bookmark_item, wrappBookmark, false);
		bm.setPositionBookmark(pos);
		LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		bm.setPadding(0, 3, 0, 3);
		bm.setLayoutParams(param);
		int count = wrappBookmark.getChildCount();
		wrappBookmark.addView(bm, count - 1);
		addBookmarkShowed(BookmarksManager.getInstance().getBookmarkByPosition(pos));
	}

	@Override
	public void onAddNewBookmark() {
		checkAdd();
	}

	private void checkAdd() {
		if(bookmarkShowed == null ){
			bookmarkDatas = BookmarksManager.getInstance().getBookmarks();
			addNewBookmark(bookmarkDatas.size() - 1); 
		}else if (bookmarkDatas.size() > bookmarkShowed.size()) {
			bookmarkDatas = BookmarksManager.getInstance().getBookmarks();
			addNewBookmark(bookmarkDatas.size() - 1);
		}
	}

}
